﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ThanhTu.GNC.Media.KQXS.DataAccess.DAL;
using ThanhTu.GNC.Media.KQXS.DataAccess.DO;
using ThanhTu.GNC.Media.KQXS.DataAccess.BL;
using System.Data;

namespace GNCMedia.KQXS.MenuPage
{
    public partial class Dudoan2_3Capso : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ProvinceBL vprovince = new ProvinceBL();
            PredictedBL vPredictedBL = new PredictedBL();
            PredictedDO PredictedDO = new PredictedDO();
            try
            {
                if (!IsPostBack)
                {

                    List<ProvinceDO> list_province = vprovince.selectadd();

                    ddl_dudoan_2cap.DataSource = list_province;
                    ddl_dudoan_2cap.DataTextField = "princeName";
                    ddl_dudoan_2cap.DataValueField = "provinceID";
                    ddl_dudoan_2cap.DataBind();

                    ddl_dudoan_3cap.DataSource = list_province;
                    ddl_dudoan_3cap.DataTextField = "princeName";
                    ddl_dudoan_3cap.DataValueField = "provinceID";
                    ddl_dudoan_3cap.DataBind();

                    Rpt_2cap.DataSource = vPredictedBL.GetPredicted(DateTime.Now, ddl_dudoan_2cap.SelectedValue, "3");
                    Rpt_2cap.DataBind();
                    Rpt_3cap.DataSource = vPredictedBL.GetPredicted(DateTime.Now, ddl_dudoan_3cap.SelectedValue, "4");
                    Rpt_3cap.DataBind();

                    lbl_tong_2cap.Text = vPredictedBL.GetALLPredicted(DateTime.Now, ddl_dudoan_2cap.SelectedValue, "3").Rows.Count.ToString();
                    lbl_tong_3cap.Text = vPredictedBL.GetALLPredicted(DateTime.Now, ddl_dudoan_3cap.SelectedValue, "4").Rows.Count.ToString();
                    lbl_provin_2cap.Text = ddl_dudoan_2cap.SelectedItem.Text;
                    lbl_province_3cap.Text = ddl_dudoan_3cap.SelectedItem.Text;
                    lbl_province_2cap_2.Text = ddl_dudoan_2cap.SelectedItem.Text;
                    lbl_province_3cap_2.Text = ddl_dudoan_3cap.SelectedItem.Text;
                    int max_cap3 = 0;
                    int max_cap2 = 0;
                    int min_cap3 = 1000000;
                    int min_cap2 = 1000000;
                    string smax_cap3 = string.Empty;
                    string smax_cap2 = string.Empty;
                    string smin_cap2 = string.Empty;
                    string smin_cap3 = string.Empty;
                    foreach (DataRow row in vPredictedBL.GetPredicted(DateTime.Now, ddl_dudoan_2cap.SelectedValue, "3").Rows)
                    {
                        if (Convert.ToInt32(row["Count_number"].ToString()) >= max_cap2)
                        {
                            max_cap2 = Convert.ToInt32(row["Count_number"].ToString());
                            smax_cap2 = row["NUMBER_PREDICTED"].ToString();
                        }
                        if (Convert.ToInt32(row["Count_number"].ToString()) <= min_cap2)
                        {
                            min_cap2 = Convert.ToInt32(row["Count_number"].ToString());
                            smin_cap2 = row["NUMBER_PREDICTED"].ToString();
                        }
                    }
                    foreach (DataRow row in vPredictedBL.GetPredicted(DateTime.Now, ddl_dudoan_3cap.SelectedValue, "4").Rows)
                    {
                        if (Convert.ToInt32(row["Count_number"].ToString()) >= max_cap3)
                        {
                            max_cap3 = Convert.ToInt32(row["Count_number"].ToString());
                            smax_cap3 = row["NUMBER_PREDICTED"].ToString();
                        }
                        if (Convert.ToInt32(row["Count_number"].ToString()) <= min_cap3)
                        {
                            min_cap3 = Convert.ToInt32(row["Count_number"].ToString());
                            smin_cap3 = row["NUMBER_PREDICTED"].ToString();
                        }
                    }
                    lbl_max_2cap.Text = smax_cap2;
                    lbl_min_2cap.Text = smin_cap2;
                    lbl_max_3cap.Text = smax_cap3;
                    lbl_min_3cap.Text = smin_cap3;
                }
            }
            catch (Exception ex)
            {
                Response.Write("Error load province at phan tich 2 cap so:" + ex);
            }
        }

        protected void Ibt_dudoan_2capso_Click(object sender, ImageClickEventArgs e)
        {
            PredictedBL vPredictedBL = new PredictedBL();
            PredictedDO PredictedDO = new PredictedDO();
            try
            {
                if (txt_2cap_1.Text.Trim().Length != 2)
                {
                    Response.Write(@"<script language='javascript'>alert('số bạn dự đoán phải có 2 chữ số');</script>");
                    return;
                }
                if (txt_2cap_2.Text.Trim().Length != 2)
                {
                    Response.Write(@"<script language='javascript'>alert('số bạn dự đoán phải có 2 chữ số');</script>");
                    return;
                }
                PredictedDO.Province_id = ddl_dudoan_2cap.SelectedValue;
                PredictedDO.Number_predicted = txt_2cap_1.Text + "-" + txt_2cap_2.Text;
                PredictedDO.Predicted_date = DateTime.Now;
                PredictedDO.Stylepredicted_Id = 3;
                vPredictedBL.SavePredicted(PredictedDO);


                Rpt_2cap.DataSource = vPredictedBL.GetPredicted(DateTime.Now, ddl_dudoan_2cap.SelectedValue, "3");
                Rpt_2cap.DataBind();

                lbl_tong_2cap.Text = vPredictedBL.GetALLPredicted(DateTime.Now, ddl_dudoan_2cap.SelectedValue, "3").Rows.Count.ToString();
                lbl_provin_2cap.Text = ddl_dudoan_2cap.SelectedItem.Text;
                lbl_province_2cap_2.Text = ddl_dudoan_2cap.SelectedItem.Text;
                int max_cap2 = 0;
                int min_cap2 = 1000000;
                string smax_cap2 = string.Empty;
                string smin_cap2 = string.Empty;
                foreach (DataRow row in vPredictedBL.GetPredicted(DateTime.Now, ddl_dudoan_2cap.SelectedValue, "3").Rows)
                {
                    if (Convert.ToInt32(row["Count_number"].ToString()) >= max_cap2)
                    {
                        max_cap2 = Convert.ToInt32(row["Count_number"].ToString());
                        smax_cap2 = row["NUMBER_PREDICTED"].ToString();
                    }
                    if (Convert.ToInt32(row["Count_number"].ToString()) <= min_cap2)
                    {
                        min_cap2 = Convert.ToInt32(row["Count_number"].ToString());
                        smin_cap2 = row["NUMBER_PREDICTED"].ToString();
                    }
                }
                lbl_max_2cap.Text = smax_cap2;
                lbl_min_2cap.Text = smin_cap2;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void Ibt_dudoan_3cap_Click(object sender, ImageClickEventArgs e)
        {
            PredictedBL vPredictedBL = new PredictedBL();
            PredictedDO PredictedDO = new PredictedDO();
            try
            {
                if (txt_3cap_1.Text.Trim().Length != 2)
                {
                    Response.Write(@"<script language='javascript'>alert('số bạn dự đoán phải có 2 chữ số');</script>");
                    return;
                }
                if (txt_3cap_2.Text.Trim().Length != 2)
                {
                    Response.Write(@"<script language='javascript'>alert('số bạn dự đoán phải có 2 chữ số');</script>");
                    return;
                }
                if (txt_3cap_3.Text.Trim().Length != 2)
                {
                    Response.Write(@"<script language='javascript'>alert('số bạn dự đoán phải có 2 chữ số');</script>");
                    return;
                }
                PredictedDO.Province_id = ddl_dudoan_3cap.SelectedValue;
                PredictedDO.Number_predicted = txt_3cap_1.Text + "-" + txt_3cap_2.Text + "-" + txt_3cap_3.Text;
                PredictedDO.Predicted_date = DateTime.Now;
                PredictedDO.Stylepredicted_Id = 4;
                vPredictedBL.SavePredicted(PredictedDO);

                Rpt_3cap.DataSource = vPredictedBL.GetPredicted(DateTime.Now, ddl_dudoan_3cap.SelectedValue, "4");
                Rpt_3cap.DataBind();

                lbl_tong_3cap.Text = vPredictedBL.GetALLPredicted(DateTime.Now, ddl_dudoan_3cap.SelectedValue, "4").Rows.Count.ToString();

                lbl_province_3cap.Text = ddl_dudoan_3cap.SelectedItem.Text;
                lbl_province_3cap_2.Text = ddl_dudoan_3cap.SelectedItem.Text;
                int max_cap3 = 0;
                int min_cap3 = 1000000;
                string smax_cap3 = string.Empty;
                string smin_cap3 = string.Empty;
                foreach (DataRow row in vPredictedBL.GetPredicted(DateTime.Now, ddl_dudoan_3cap.SelectedValue, "4").Rows)
                {
                    if (Convert.ToInt32(row["Count_number"].ToString()) >= max_cap3)
                    {
                        max_cap3 = Convert.ToInt32(row["Count_number"].ToString());
                        smax_cap3 = row["NUMBER_PREDICTED"].ToString();
                    }
                    if (Convert.ToInt32(row["Count_number"].ToString()) <= min_cap3)
                    {
                        min_cap3 = Convert.ToInt32(row["Count_number"].ToString());
                        smin_cap3 = row["NUMBER_PREDICTED"].ToString();
                    }
                }
                lbl_max_3cap.Text = smax_cap3;
                lbl_min_3cap.Text = smin_cap3;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void btn_search_3cap_Click(object sender, EventArgs e)
        {
            PredictedBL vPredictedBL = new PredictedBL();
            foreach (DataRow row in vPredictedBL.GetPredicted(DateTime.Now, ddl_dudoan_3cap.SelectedValue, "4").Rows)
            {
                if (row["NUMBER_PREDICTED"].ToString() == txt_search_3cap.Text)
                {
                    string mess = "Số dự đoán :" + row["NUMBER_PREDICTED"].ToString() + " Lượt dự đoán :" + row["Count_number"].ToString();
                    Response.Write(@"<script language='javascript'>alert('" + mess + "');</script>");
                }
            }
        }

        protected void btn_search_2cap_Click(object sender, EventArgs e)
        {
            PredictedBL vPredictedBL = new PredictedBL();
            foreach (DataRow row in vPredictedBL.GetPredicted(DateTime.Now, ddl_dudoan_2cap.SelectedValue, "3").Rows)
            {
                if (row["NUMBER_PREDICTED"].ToString() == txt_search_2cap.Text)
                {
                    string mess = "Số dự đoán :" + row["NUMBER_PREDICTED"].ToString() + " Lượt dự đoán :" + row["Count_number"].ToString();
                    Response.Write(@"<script language='javascript'>alert('" + mess + "');</script>");
                }
            }
        }

    }
}